package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Comment;
import com.qianniu.napi.admin.service.ICommentService;
import com.qianniu.napi.admin.service.IGoodsfavService;
import com.qianniu.napi.admin.service.IOrderGoodsService;
import com.qianniu.napi.admin.service.IOrderService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 评论
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-06
 */
@Controller
public class CommentController {

    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderGoodsService orderGoodsService;

    @Autowired
    private IGoodsfavService goodsfavService;

    @RequestMapping("admin/comment/index")
    public String index() {
        return "admin/comment";
    }

    @RequestMapping("/admin/comment/list")
    @ResponseBody
    public Page<Comment> list(@RequestParam(value = "offset", required = false) Integer offset,
                              @RequestParam(value = "status", required = false) Integer status,
                              @RequestParam(value = "search", required = false) String search,
                              @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Comment> wrapper = new EntityWrapper<Comment>();
        wrapper.eq("mid", ObjectUtil.getMid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search).or().like("service_code", search)
//                    .or().like("address", search).or().like("linkman", search).or().like("phone", search);

        wrapper.orderBy("id", false);
        Page<Comment> page = commentService.selectPage(new Page<Comment>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/comment/add")
    public String add(@Valid Comment bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/comment";
    }

    @RequestMapping("/admin/comment/getCommentById")
    @ResponseBody
    public Comment getCommentById(long id) {
        Comment comment = commentService.selectById(id);
        return comment;
    }

    @RequestMapping("/admin/comment/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = commentService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/comment/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Comment bean = new Comment();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        commentService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/comment/modify")
    public String update(Comment bean) {
        bean.setModifytime(new Date());
        boolean isSuccc = commentService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
